package com.fm.financemanage.controller;

import com.fm.financemanage.component.TokenComponent;
import com.fm.financemanage.constant.ResponseEnum;
import com.fm.financemanage.constant.annotation.Require;
import com.fm.financemanage.exception.ParameterException;
import com.fm.financemanage.model.po.SysUser;
import com.fm.financemanage.model.vo.PageVo;
import com.fm.financemanage.model.vo.accountant.AccountantListVo;
import com.fm.financemanage.model.vo.accountant.AccountantOperatorVo;
import com.fm.financemanage.model.vo.simcard.SimCardListVo;
import com.fm.financemanage.model.vo.simcard.SimCardNoticeListVo;
import com.fm.financemanage.model.vo.simcard.SimCardOpeartorVo;
import com.fm.financemanage.response.Callback;
import com.fm.financemanage.response.JsonData;
import com.fm.financemanage.service.AccountantService;
import com.fm.financemanage.service.SimCardService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.util.List;

@RequestMapping("/sim/card")
@Api(tags = "sim卡")
@RestController
@Validated
public class SimCardController {

    @Autowired
    private SimCardService simCardService;

    @Autowired
    private TokenComponent tokenComponent;
    @Require
    @GetMapping("/")
    @ApiOperation("查询卡列表")
    public JsonData<PageVo<List<SimCardListVo>>> getList(HttpServletRequest request, HttpServletResponse response,
                                                         @ApiParam(name = "accountantId", value = "会计员Id", required = true)
                                                         @RequestParam(name = "accountantId", required = false) Integer accountantId,
                                                         @ApiParam(name = "simCardNumber", value = "号码", required = true)
                                                         @RequestParam(name = "simCardNumber", required = false) String simCardNumber,
                                                         @ApiParam(name = "page", value = "页码", required = true)
                                                         @RequestParam(name = "page", required = false) Integer page,
                                                         @ApiParam(name = "limit", value = "条数", required = true)
                                                         @RequestParam(name = "limit", required = false) Integer limit
    ) {
        final String uuid = request.getAttribute("uuid").toString();

        return Callback.success(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMemo(), simCardService.getList(uuid, simCardNumber, accountantId, page, limit));
    }

    @Require
    @PostMapping("/")
    @ApiOperation("")
    public JsonData operator(HttpServletRequest request, HttpServletResponse response,
                             @RequestBody SimCardOpeartorVo simCardOpeartorVo, BindingResult bindingResult
    ) {
        if (bindingResult.hasErrors()) {
            throw new ParameterException(bindingResult.getAllErrors());
        }
        final String uuid = request.getAttribute("uuid").toString();
        simCardOpeartorVo.setUserUuid(uuid);
        simCardService.operator(simCardOpeartorVo);
        return Callback.success(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMemo());
    }

    @Require
    @DeleteMapping("/{id}")
    @ApiOperation("删除")
    public JsonData operator(HttpServletRequest request, HttpServletResponse response,
                             @PathVariable("id") Integer id
    ) {
        simCardService.removeById(id);
        return Callback.success(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMemo());
    }

    @Require
    @GetMapping("/notice/record")
    @ApiOperation("查询sim短信日志")
    public JsonData<PageVo<List<SimCardNoticeListVo>>> getNoticeList(HttpServletRequest request, HttpServletResponse response,
                                                                     @ApiParam(name = "accountantId", value = "会计员Id", required = true)
                                                                     @RequestParam(name = "accountantId", required = false) Integer accountantId,
                                                                     @ApiParam(name = "simCardId", value = "sim卡Id", required = true)
                                                                     @RequestParam(name = "IntegersimCardId", required = false) Integer simCardId,
                                                                     @ApiParam(name = "page", value = "页码", required = true)
                                                                     @RequestParam(name = "page", required = false) Integer page,
                                                                     @ApiParam(name = "limit", value = "条数", required = true)
                                                                     @RequestParam(name = "limit", required = false) Integer limit
    ) {
        final String uuid = request.getAttribute("uuid").toString();

        return Callback.success(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMemo(), simCardService.getNoticeRecordList(uuid, simCardId, accountantId, page, limit));
    }

    @Require
    @DeleteMapping("/notice/record/{id}")
    @ApiOperation("删除sim短信日志")
    public JsonData<PageVo<List<SimCardNoticeListVo>>> removeNotice(HttpServletRequest request, HttpServletResponse response, @PathVariable("id") Integer id
    ) {

        simCardService.removeNotice(id);
        return Callback.success(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMemo());
    }




}
